#include "isearch.h"

#include "gt_entry.h"
#include "gt_option.h"
#include <cstdlib>
#include <cstring>

// ok
void do_git_search(search_t *search);
void do_ag_search(search_t *search);
void do_grep_search(search_t *search);

// ok
void do_search(search_t *search) {
  switch (search->option->search_type) {
  case GREP_SEARCH:
    return do_grep_search(search);
  case AG_SEARCH:
    return do_ag_search(search);
  case GIT_SEARCH:
    return do_git_search(search);
  default:
    break;
  }

  exit(-1);
}

// ok
search_t *create_search(option_t *opt) {
  result_t *result = (result_t *)calloc(1, sizeof(*result));
  result->entries = NULL;
  result->start = result->entries;
  result->nbentry = 0;
  search_t *search = (search_t *)calloc(1, sizeof(*search));
  search->result = result;
  search->status = 1;
  search->option = opt;
  return search;
}

// ok
void free_search(search_t *search) {
  entry_t *ptr = search->result->start;
  entry_t *p;

  while (ptr) {
    p = ptr;
    ptr = ptr->next;
    free_entry(p);
  }
  free(search->result);

  if (search->option)
    free(search->option);

  free(search);
}